from django.db import models
from utils.base import BaseModel


class Case(BaseModel):

    class CaseType(models.IntegerChoices):
        API = 1, 'API'
        UI = 2, 'UI'
    name = models.CharField(verbose_name='用例名称', max_length=20)
    type = models.IntegerField(verbose_name='用例类型', default=CaseType.API, choices=CaseType.choices)
    tag = models.ManyToManyField('Tag', help_text='标签')
    project = models.ForeignKey('projects.Project', on_delete=models.PROTECT)
    module = models.ForeignKey('envs.Module', on_delete=models.PROTECT)
    steps = models.JSONField(verbose_name='用例步骤')
    params = models.JSONField(verbose_name='用例全局变量设置')
    setup = models.JSONField(verbose_name='用例前置步骤')
    teardown = models.JSONField(verbose_name='用例后置步骤')

    class Meta:
        db_table = 'tb_case'
        verbose_name = '用例表'
        verbose_name_plural = verbose_name


class Tag(BaseModel):
    project = models.ForeignKey('projects.Project', on_delete=models.PROTECT)
    name = models.CharField(verbose_name='标签名称', max_length=20)

    class Meta:
        ordering = ['-create_time']
        db_table = 'tb_tag'
        verbose_name = '标签表'
        verbose_name_plural = verbose_name


class Step(BaseModel):
    name = models.CharField(verbose_name='步骤名称', max_length=20)
    project = models.ForeignKey('projects.Project', on_delete=models.PROTECT)
    module = models.ForeignKey('envs.Module', on_delete=models.PROTECT)
    steps = models.JSONField(verbose_name='用例步骤')

    class Meta:
        ordering = ['-create_time']
        db_table = 'tb_step'
        verbose_name = '步骤表'
        verbose_name_plural = verbose_name


class CaseRunLog(BaseModel):

    class CaseResult(models.IntegerChoices):
        SUCCESS = 1, '成功'
        FAIL = 2, '失败'
        ERROR = 3, '错误'

    case = models.ForeignKey(Case, on_delete=models.CASCADE)
    report = models.ForeignKey('reports.Report', on_delete=models.DO_NOTHING, null=True)
    logs = models.JSONField(verbose_name='用例执行日志')
    result = models.IntegerField(verbose_name='用例执行结果', default=CaseResult.SUCCESS, choices=CaseResult.choices)
    time = models.FloatField(verbose_name='用例执行时间')

    class Meta:
        db_table = 'tb_case_run_log'
        verbose_name = '用例执行日志表'
        ordering = ['-create_time']
        verbose_name_plural = verbose_name
